<?php
/**
 * Image manipulation library.
 *
 * @author Martin "DevelX" Jurča
 * @version 2009-03-20
 */
class Image {
    private static $image = false;
    private static $width = false;
    private static $height = false;

    public static function render(&$parameters, $output) {
        foreach ($parameters as $params) {
            switch ($params['cmd']) {
                case 'import':
                    self::import($params[0]);
                    break;
                case 'save':
                    self::save($params[0], $params[1], $params[2]);
                    break;
                case 'resize':
                    self::resize($params[0], $params[1], $params[2],
                            $params[3], $params[4]);
                    break;
                case 'watermark':
                    self::watermark($params[0], $params[1], $params[2],
                            $params[3]);
                    break;
                case 'crop':
                    self::crop($params[0], $params[1], $params[2], $params[3]);
                    break;
                case 'filter':
                    self::filter($params[0], $params[1]);
                    break;
                case 'rotate':
                    self::rotate($params[0], $params[1]);
                    break;
            }
        }
        if ($output === false) {
            @imagejpeg(self::$image, null, Config::IMAGE_QUALITY);
        } else {
            $format = substr($output, -3);
            self::save($output, $format);
        }
    }

    public static function import($url) {
        $details = @getimagesize($url);
        if ($details !== false) {
            $image = &self::loadImage($url, $details[2]);
            if ($image === false) {
                return false;
            }
            if (self::$image !== false) {
                @imagedestroy(self::$image);
            }
            self::$width = $details[0];
            self::$height = $details[1];
            self::$image = $image;
            return true;
        }
        return false;
    }

    public static function resize($width, $height, $border = false,
            $backgroundColor = -1, $keepAspectRatio = true) {
        $newWidth = $width;
        $newHeight = $height;
        $posX = 0;
        $posY = 0;
        $originalAspectRatio = self::$width / self::$height;
        $newAspectRatio = $newWidth / $newHeight;
        if ($keepAspectRatio && ($newAspectRatio != $originalAspectRatio)) {
            if ($originalAspectRatio > $newAspectRatio) {
                $newHeight = round($height / $originalAspectRatio *
                        $newAspectRatio);
            } else {
                $newWidth = round($width / $newAspectRatio *
                        $originalAspectRatio);
            }
        }
        if ($border) {
            $image = @imagecreatetruecolor($width, $height);
            if ($originalAspectRatio > $newAspectRatio) {
                $posY = floor(($height - $newHeight) / 2);
            } elseif ($originalAspectRatio < $newAspectRatio) {
                $posX = floor(($width - $newWidth) / 2);
            }
            if ($backgroundColor == -1) {
                $bgCol = @imagecolorallocate($image,
                    Config::IMAGE_BACKGROUND_R, Config::IMAGE_BACKGROUND_G,
                    Config::IMAGE_BACKGROUND_B);
            } else {
                $bgCol = @imagecolorallocate($image, $backgroundColor[0],
                    $backgroundColor[1], $backgroundColor[2]);
            }
            @imagefilledrectangle($image, 0, 0, $width, $height, $bgCol);
        } else {
            $image = @imagecreatetruecolor($newWidth, $newHeight);
            if ($backgroundColor == -1) {
                $bgCol = @imagecolorallocate($image,
                    Config::IMAGE_BACKGROUND_R, Config::IMAGE_BACKGROUND_G,
                    Config::IMAGE_BACKGROUND_B);
            } else {
                $bgCol = @imagecolorallocate($image, $backgroundColor[0],
                    $backgroundColor[1], $backgroundColor[2]);
            }
            @imagefilledrectangle($image, 0, 0, $width, $height, $bgCol);
        }
        $result = @imagecopyresampled($image, self::$image, $posX, $posY, 0, 0,
                $newWidth, $newHeight, self::$width, self::$height);
        @imagedestroy(self::$image);
        self::$image = $image;
        return $result;
    }

    public static function save($url, $format = 'jpg', $quality = -1) {
        FTP::createFile($url);
        $format = strtolower($format);
        switch ($format) {
            case 'png':
                return @imagepng(self::$image, $url, 9);
                break;
            case 'gif':
                return @imagegif(self::$image, $url);
                break;
            case 'bmp':
                return @imagewbmp(self::$image, $url);
                break;
            default:
                return @imagejpeg(self::$image, $url, $quality == -1 ?
                        Config::IMAGE_QUALITY : $quality);
                break;
        }
    }

    public static function watermark($watermarkUrl, $x, $y, $opacity = 100) {
        $details = @getimagesize($watermarkUrl);
        $watermark = &self::loadImage($watermarkUrl, $details[2]);
        $result = @imagecopymerge(self::$image, $details, $x, $y, 0, 0,
                self::$width, self::$height, $opacity);
        return $result;
    }

    public static function crop($width, $height, $shiftX, $shiftY) {
        $image = @imagecreatetruecolor($width, $height);
        $color = @imagecolorallocate($image, Config::IMAGE_BACKGROUND_B,
                Config::IMAGE_BACKGROUND_G, Config::IMAGE_BACKGROUND_B);
        @imagefilledrectangle($image, 0, 0, $width, $height, $color);
        $result = @imagecopy($image, self::$image, $shiftX, $shiftY, 0, 0,
                self::$width, self::$height);
        @imagedestroy(self::$image);
        self::$image = $image;
        return $result;
    }

    public static function filter($filter, $args) {
        return @imagefilter(self::$image, $filter, $args[0], $args[1],
                $args[2], $args[3]);
    }

    public static function rotate($angle, $background = -1) {
        if ($background == -1) {
            $color = @imagecolorallocate(self::$image,
                    Config::IMAGE_BACKGROUND_R, Config::IMAGE_BACKGROUND_G,
                    Config::IMAGE_BACKGROUND_B);
        } else {
            $color = @imagecolorallocate(self::$image, $background[0],
                    $background[1], $background[2]);
        }
        return @imagerotate(self::$image, $angle,
                $background == -1 ? Config::IMAGE_BACKGROUND : $background);
    }

    public static function getWidth() {
        return self::$width;
    }

    public static function getHeight() {
        return self::$height;
    }

    /**
     * Loads the image.
     *
     * @param String $url URL to the image.
     */
    private static function &loadImage($url, $type) {
        $image = null;
        switch ($type) {
            case IMAGETYPE_WBMP:
                $image = @imagecreatefromwbmp($url);
                break;
            case IMAGETYPE_GIF:
                $image = @imagecreatefromgif($url);
                break;
            case IMAGETYPE_JPEG:
                $image = @imagecreatefromjpeg($url);
                break;
            case IMAGETYPE_PNG:
                $image = @imagecreatefrompng($url);
                break;
            case IMAGETYPE_XBM:
                $image = @imagecreatefromxbm($url);
                break;
            default:
                return false;
                break;
        }
        @imagealphablending($image, true);
        return $image;
    }
}
?>
